package cn.usersearch.controller;


import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import cn.usersearch.bean.UserBean;
import cn.usersearch.service.LoginService;


@Controller("LoginController")
@RequestMapping("/")
public class LoginController {
	@Autowired
	LoginService LoginService;

	@RequestMapping(value = "/", method = RequestMethod.GET)
    public String index(Model model) {
		model.addAttribute("message", "请输入您的用户名和密码 :");
        return "index";
    }
    //用户登录   
	
	 @RequestMapping(value = "/init", method = RequestMethod.POST, params="login")
	    public String initLogin(UserBean userBean, Model model) {
	    	List<UserBean> result = LoginService.searchUser(userBean);
	    	if(result.size() != 0) {
	    		List<UserBean> result1 = LoginService.searchUser(null);
	    		model.addAttribute("list",result1);
	    		return "information";//登录成功 进入列表页
	    	} else
	    	    model.addAttribute("message","用户名或密码错误，请重新输入");
	    		return "index";//登录失败返回登录页面
	    	}
	 //新用户注册
	 @RequestMapping(value = "/init", method = RequestMethod.POST, params="register")
	    public String initLogin(Model model) {
	 			return "register";
	 }
	    @RequestMapping(value = "/init1", method = RequestMethod.POST,params="insert")
	    public String initinsert(UserBean userBean,Model model) {
	    	if(userBean.getPassWord().equals(userBean.getPassWord1()))
	    	{   List<UserBean> result1 = LoginService.searchUser(userBean);
	    	    if(result1.size()!=0){
	       	   model.addAttribute("message", "您注册的ID已被别人使用！！请重新注册");
	       	   return "register";}
	       	   
	    	 else{ 
    		     if( LoginService.searchinsert(userBean)!=0){
    		    	 model.addAttribute("message", "注册成功！");
		          return "index";}
    		     else{ 
   		          return "helloWord";}
	    	 }
	    	   
    	}
    	else {
      	   model.addAttribute("message", "确认密码错误！！！！请重新输入您的密码并确认");
      	   return "register";}
}
	     @RequestMapping(value = "/init2", method = RequestMethod.POST)
	    public String initupdate(UserBean userBean) {
	    	
	    	if( LoginService.searchupdate(userBean)!=0) return "updatepage";
	    	else  
	    		return "helloWord";
	     }
	    @RequestMapping(value = "/init3", method = RequestMethod.POST,params="delete")
	    public String initdelete(UserBean userBean,Model model) {
	    	
	    	if( LoginService.searchdelete(userBean)!=0) 
	    		{List<UserBean> result = LoginService.searchUser(null);
	    	      model.addAttribute("list", result);
	    	     return "information";
	    		}
	    	else  
	    		return "deletefalse";
	    }
	    @RequestMapping(value = "/init4", method = RequestMethod.POST)
	    public String initregister(Model model) {
	    		return "register";
	    }
	    @RequestMapping(value = "/init5", method = RequestMethod.GET)
	    public String initdeleteconfrim(UserBean userBean,Model model) {
	    	model.addAttribute("userBean", userBean);
	    		return "deleteconfrim";
	    }
	    @RequestMapping(value = "/init6", method = RequestMethod.POST)
	    public String initsearch(UserBean userBean,Model model) {
	    	List<UserBean> result1 = LoginService.searchUser(null);
			model.addAttribute("list",result1);
			return "information";
	    }
    /*   @RequestMapping(value = "/init7", method = RequestMethod.POST)
	    public String initsearch1(UserBean userBean,Model model) {
	    	LoginService.searchupdate(userBean);
	    	List<UserBean> result1 = LoginService.searchUser(null);
			model.addAttribute("list",result1);
			return "information";
	    }
	    @RequestMapping(value = "/init8", method = RequestMethod.GET)
	    public String updateUserInformation(UserBean userBean,Model model) {
	    	UserBean result=LoginService.searchUserId(userBean);
	    	model.addAttribute("userBean", result);
	    		return "deleteconfrim";
	    		
	    }*/
}
